angular.module('base', [
    'templates'
    'constants'
    'Controller'
    'ngRoute'
    'ngAnimate'
    'ngSanitize'
    'ui.bootstrap'
    'easypiechart'
    'textAngular'
    'ui.tree'
    'ngMap'
    'ngTagsInput'
    'slick'
    'ui.select'
    'selectApp'
    'ui.calendar'
    'tablesorting'
    'inlineedittable'
    'TodoApp'
    'app.controllers'
    'app.directives'
    'app.localization'
    'app.nav'
    'app.ui.ctrls'
    'app.ui.directives'
    'app.form.validation'
    'app.ui.form.ctrls'
    'app.ui.form.directives'
    'app.tables'
    'app.task'
    'app.chart.ctrl'
    'app.chart.directives'
    'angular-animate'
    'app.calendar'
    'app.ui.jvectormap'
    'xeditable'
    'FullscreenApp'
    'galleryApp'
    'datatables'
])

.run () ->
    $url = location.href
    $qIndex = $url.indexOf('?')
    $hashIndex = $url.indexOf('#')
    if $qIndex < $hashIndex
        $urlArr = $url.substr($qIndex + 1, $hashIndex - $qIndex - 1).split('&')
    else
        $urlArr = $url.substr($qIndex + 1).split('&')
    $params = {}
    for $str in $urlArr
        $key = $str.split('=')[0]
        $value = $str.split('=')[1]
        $params[$key] = $value
.config ($routeProvider) ->
    routes = [
        'dashboard/dashboard-v1'
        'ui/typography'
        'ui/buttons'
        'ui/icons'
        'ui/grids'
        'ui/widgets'
        'ui/components'
        'ui/timeline'
        'ui/nested-lists'
        'ui/fontawesome'
        'ui/animation'
        'ui/panel'
        'ui/xeditable'
        'maps/gmap'
        'maps/vectormap'
        'tables/static'
        'tables/dynamic'
        'tables/responsive'
        'forms/elements'
        'forms/layouts'
        'forms/validation'
        'forms/select'
        'forms/wizard'
        'charts/charts'
        'charts/flot'
        'charts/morris'
        'charts/highcharts'
        'pages/404'
        'pages/500'
        'pages/blank'
        'pages/forgot-password'
        'pages/invoice'
        'pages/lock-screen'
        'pages/profile'
        'pages/signin'
        'pages/signup'
        'pages/directory'
        'pages/faq'
        'pages/gallery'
        'mail/compose'
        'mail/inbox'
        'mail/mailview'
        'tasks/tasks'
        'calender/calender'
        'ecommerce/dashboard'
        'ecommerce/orders'
        'ecommerce/order-views'
        'ecommerce/products'
        'ngtable/ngtable'
        'settings/settings-panel'
    ]

    setRoutes = (route) ->
        url = '/' + route;
        config = {
            templateUrl: 'views/' + route + '.html'
        }
        $routeProvider.when(url, config)
        $routeProvider

    routes.forEach (route) ->
        setRoutes(route)
    $routeProvider.when '/', {redirectTo: 'dashboard/dashboard-v1'}
    .when '/404', {templateUrl: 'views/pages/404.html'}
    .otherwise {redirectTo: '/404'}
.run () ->
#重写tofix函数四舍五入
    Number.prototype.toFixed = (d)->
        s = this + ""
        if !d then d = 0
        if s.indexOf(".") == -1 then s += "."
        s += new Array(d + 1).join("0")
        if new RegExp("^(-|\\+)?(\\d+(\\.\\d{0," + (d + 1) + "})?)\\d*$").test(s)
            s = "0" + RegExp.$2
            pm = RegExp.$1
            a = RegExp.$3.length
            b = true
            if a == d + 2
                a = s.match(/\d/g)
                if parseInt(a[a.length - 1]) > 4
                    for i in [a.length - 2..0]
                        a[i] = parseInt(a[i]) + 1
                        if a[i] == 10
                            a[i] = 0
                            b = i != 1
                        else
                            break
                s = a.join("").replace(new RegExp("(\\d+)(\\d{" + d + "})\\d$"), "$1.$2")
            if b then s = s.substr(1)
            return (pm + s).replace(/\.$/, "")
        return this + ""

    Event.prototype.addEvent = (obj, types, handle)->
        types = types.split(' ')
        for type in types
            try
                obj.addEventListener(type, handle, false)
            catch
                try
                    obj.attachEvent('on', type, handle)
                catch
                    obj['on' + type] = handle

    Event.prototype.removeEvent = (obj, types, handle)->
        types = types.split(' ')
        for type in types
            try
                obj.removeEventListener(type, handle, false)
            catch
                try
                    obj.detachEvent('on', type, handle)
                catch
                    obj['on' + type] = null

angular.module('Controller', [

])

